using System;
using System.Collections;
using System.Collections.Generic;
using EventUtils;
using UnityEngine;

public class PropItemTable
{
    public int tableindex;
    public PropItemEnum enumid;
    public int Id;
    //物品名称
    public string itemname;
    //地址
    public string path;
    //图片名称
    public string iconname;
    //物品描述
    public string desc;
    // //使用次数
    public int count;
    /// <summary>
    /// 物品类型
    /// 0是复活道具不参加商城活动
    /// 1是道具类型，在商城售卖
    /// 2是使用类型，金币和体力
    /// </summary>
    public int itemType;
    //使用一次物品之后增加多少
    public int useadd;
    //购买需要多少前
    public int buyneed;
}

//设置的item
public enum PropItemEnum
{
    none,
    xiaochu,//消除
    xipai,//洗牌
    fanpai,//翻牌
    citie,//磁铁
    huitui,//回退
    jiashi,//加时
    tili,//体力
    jinbi,//井壁
    fuhuo,//游戏模式 1 集齐七个麻将之后没有格子了，表示死亡，可以复活一次
}

public class GameLocalInfo
{
    /// <summary>
    /// 用户的金币
    /// </summary>
    public static float mPlayerGold = 0;
    /// <summary>
    /// 用户体力
    /// </summary>
    public static float mPlayerPower = 0;

    //最大体力
    public static float mPlayerPowerMax = 10;
    
    /// <summary>
    /// 获取一个幸运奖励所需要的时长
    /// </summary>
    public static int mGetLuckyNeedTime = 3600;
    //如果是unity只需要等待六十秒
#if UNITY_EDITOR
    public static int mGetDepotPowerNeedTime = 600;
    #else
    public static int mGetDepotPowerNeedTime = 300;//五分钟
#endif
    
    /// <summary>
    /// 登陆的 时候的时间戳
    /// </summary>
    public static long mLoginLuckyTimer = 0;
    ////////////////////////////////////////////////

    #region 设置和保存本地 bgm

    private static string LocalBGMString = "Local_BGM_String_1";
    //获取是否播放bgm
    public static bool OnIsPlayBGM()
    {
        if (PlayerPrefs.HasKey(LocalBGMString))
        {
            return PlayerPrefs.GetInt(LocalBGMString) == 1;
        }

        return true;
    }
    public static void OnSetIsPlayBGM(bool isopen)
    {
        PlayerPrefs.SetInt(LocalBGMString, isopen ? 1 : 0);
    }

    #endregion

    #region 设置和保存 sound

    private static string LocalEffectString = "Local_Effect_String_1";
    public static bool OnIsPlayEffect()
    {
        if (PlayerPrefs.HasKey(LocalEffectString))
        {
            return PlayerPrefs.GetInt(LocalEffectString) == 1;
        }

        return true;
    }
    public static void OnSetIsPlayEffect(bool isopen)
    {
        PlayerPrefs.SetInt(LocalEffectString, isopen ? 1 : 0);
    }

    #endregion

    #region 保存金币 和 体力 时间

    private static string LocalGoldString = "Local_Gold_String_1";
    public static float OnGetLocalGold(float setgold = 0)
    {
        if (setgold > 0)
        {
            PlayerPrefs.SetString(LocalGoldString, setgold.ToString());
        }
        else
        {
            if (PlayerPrefs.HasKey(LocalGoldString))
            {
                return float.Parse(PlayerPrefs.GetString(LocalGoldString));
            }
            else
            {
                PlayerPrefs.SetString(LocalGoldString, "500");
                return 500;
            }
        }

        return 0;
    }
    private static string LocalPowerString = "Local_Power_String_1";
    public static float OnGetLocalPower(float setgold = 0)
    {
        if (setgold > 0)
        {
            PlayerPrefs.SetString(LocalPowerString, setgold.ToString());
        }
        else
        {
            if (PlayerPrefs.HasKey(LocalPowerString))
            {
                return float.Parse(PlayerPrefs.GetString(LocalPowerString));
            }
            else
            {
                PlayerPrefs.SetString(LocalPowerString, mPlayerPowerMax.ToString());
                return mPlayerPowerMax;
            }
        }

        return 0;
    }

    #endregion

    #region 保存自己的物品到本地

    public static void OnSetSelfItemToLocal(bool isget = false)
    {
        string SaveLocalItemKey = "Save_Local_Item_Key_1";
        if (isget)
        {
            if (PlayerPrefs.HasKey(SaveLocalItemKey))
            {
                Debug.Log("自己身上的道具："+PlayerPrefs.GetString(SaveLocalItemKey));
                string[] itemdatastr = PlayerPrefs.GetString(SaveLocalItemKey).Split(",");
                for (int i = 0; i < itemdatastr.Length; i++)
                {
                    if (!string.IsNullOrWhiteSpace(itemdatastr[i]))
                    {
                        PropItemEnum itemnum = (PropItemEnum)int.Parse(itemdatastr[i]);
                        GameHelp.OnAddSelfPlayerPropItem(OnGetPropItemTableCloneById(itemnum),false);
                    }
                }
            }
            else
            {
                GameHelp.OnInitPlayerPropItemTable();
            }
        }
        else
        {
            string savestr = "";
            var itemdata = GameHelp.OnGetSelfPlayerPropItemTable();
        
            for (int i = 0; i < itemdata.Count; i++)
            {
                savestr += ((int)itemdata[i].enumid).ToString();
                if (i <= (itemdata.Count))
                {
                    savestr += ",";
                }
            }
            Debug.Log("保存本地数据："+savestr);
            PlayerPrefs.SetString(SaveLocalItemKey,savestr);
        }
    }

    #endregion
    
    #region 时间戳

    /// <summary>
    /// 获取当前时间戳
    /// </summary>
    /// <returns></returns>
    public static long OnGetNowTime()
    {
        return DateTimeOffset.Now.ToUnixTimeSeconds();
    }
    
    /// <summary>
    /// 获取当前时间戳 加上需要幸运值
    /// </summary>
    /// <returns></returns>
    public static long OnGetNowTimeToLucky()
    {
        return DateTimeOffset.Now.ToUnixTimeSeconds()+mGetLuckyNeedTime;
    }
//根据时间获取一个时间的显示
    public static string OnGetTimeStrByTimer()
    {
        if (mLoginLuckyTimer <= 0)
        {
            return "-1";
        }
        long curtime = OnGetNowTime();
        long jianjian = mLoginLuckyTimer - curtime;
        if (jianjian < 0)
        {
            return "-1";
        }
        string shijian = "";
        long day = (int)(jianjian / 86400);
        long hour = (int)((jianjian - day * 86400) / 3600);
        long min = (int)((jianjian - day * 86400 - hour * 3600) / 60);
        long sen = (jianjian - day * 86400 - hour * 3600 - min * 60);
        if (day > 0)
        {
            shijian += day + "d:";
        }
        if (hour > 0)
        {
            shijian += hour + "h:";
        }

        if (hour > 0 || min > 0)
        {
            if (min <= 9)
            {
                shijian += string.Format("0{0}:", min);
            }
            else
            {
                shijian += string.Format("{0}:", min);
            }
        }
        if (sen >= 0)
        {
            if (sen <= 9)
            {
                shijian += string.Format("0{0}", sen);
            }
            else
            {
                shijian += string.Format("{0}", sen);
            }
        }

        return shijian;
    }
    #endregion
    
    #region 刷新体力

    public static void OnGetLocalPowerTimer(bool isset = false)
    {
        string localpowertimer = "GET_LOCAL_POWER_TIMER_1";
        if (isset)
        {
            PlayerPrefs.SetString(localpowertimer,OnGetNowTime().ToString());
        }
        else
        {
            // PlayerPrefs.DeleteKey(localpowertimer);
            if (PlayerPrefs.HasKey(localpowertimer))
            {
                long shijianc = long.Parse(PlayerPrefs.GetString(localpowertimer));
                long chazhi = OnGetNowTime() - shijianc;
                int shijian = (int)(chazhi / mGetDepotPowerNeedTime);
                if (shijian >= 1)
                {
                    if ((mPlayerPower+shijian) < mPlayerPowerMax)
                    {
                        OnUpdatePlayerPower(shijian);
                        depotpowertimer = OnGetNowTimeToDepotPower() - chazhi;
                    }
                    else
                    {
                        OnUpdatePlayerPower((int)(mPlayerPowerMax-mPlayerPower));
                    }
                }
                else
                {
                    depotpowertimer = OnGetNowTimeToDepotPower()-chazhi;
                }
               
                Debug.LogError("设置时间："+shijian+","+mPlayerPower+",all:"+mPlayerPowerMax+",chaoguo:"+chazhi);
            }
        }
    }
    
    private static long depotpowertimer = 0;
    public static long OnGetNowTimeToDepotPower()
    {
        return DateTimeOffset.Now.ToUnixTimeSeconds()+mGetDepotPowerNeedTime;
    }
    public static void OnSetDepotPowerTime()
    {
        depotpowertimer = OnGetNowTimeToDepotPower();
        OnGetLocalPowerTimer();
    }
    public static string OnDepotPowerTimeSub()
    {
        if (mPlayerPower >= mPlayerPowerMax)
        {
            return "-2";
        }

        if (depotpowertimer <= 0)
        {
            //满了
            return "-1";
        }
        long curtime = OnGetNowTime();
        long jianjian = depotpowertimer - curtime;
        if (jianjian < 0)
        {
            OnUpdatePlayerPower(1);
            OnSetDepotPowerTime();
            return "-1";
        }
        string shijian = "";
        long day = (int)(jianjian / 86400);
        long hour = (int)((jianjian - day * 86400) / 3600);
        long min = (int)((jianjian - day * 86400 - hour * 3600) / 60);
        long sen = (jianjian - day * 86400 - hour * 3600 - min * 60);
        if (day > 0)
        {
            shijian += day + "d:";
        }
        if (hour > 0)
        {
            shijian += hour + "h:";
        }

        if (hour > 0 || min > 0)
        {
            if (min <= 9)
            {
                shijian += string.Format("0{0}:", min);
            }
            else
            {
                shijian += string.Format("{0}:", min);
            }
        }
        if (sen >= 0)
        {
            if (sen <= 9)
            {
                shijian += string.Format("0{0}", sen);
            }
            else
            {
                shijian += string.Format("{0}", sen);
            }
        }

        return shijian;
    }

    #endregion

    #region 加载物品的配置

    private static List<PropItemTable> mPropItemList = new List<PropItemTable>();

    public static List<PropItemTable> OnGetPropItemList()
    {
        return mPropItemList;
    }
    /// <summary>
    /// 加载物品配置表
    /// </summary>
    public static void OnLoadPropIconTable()
    {
        TextAsset poptable = ResourceManager.Instance.LoadAsset<TextAsset>("Hall/Table", "propitemtab",".txt");
        string str = poptable.text.Replace("\r\n", "");
        string[]lines = str.Split("|");
        for (int i = 0; i < lines.Length; i++)
        {
            string[] valuearr = lines[i].Split(",");
            PropItemTable info = new PropItemTable();
            int index = 0;
            info.tableindex = int.Parse(valuearr[index++]);
            int id = int.Parse(valuearr[index++]);
            info.Id = id;
            info.enumid = (PropItemEnum)(id - 100);
            info.itemname = valuearr[index++];
            info.path = valuearr[index++];
            info.iconname = valuearr[index++];
            info.desc = valuearr[index++];
            info.count = int.Parse(valuearr[index++]);
            info.itemType = int.Parse(valuearr[index++]);
            info.useadd = int.Parse(valuearr[index++]);
            info.buyneed = int.Parse(valuearr[index++]);
            // info.buyneed = int.Parse(valuearr[index++]);
            mPropItemList.Add(info);
        }
    }

    //获取配置表中的一条数据  index 从0开始
    public static PropItemTable OnGetPropItemTableCloneById(PropItemEnum index)
    {
        if (mPropItemList.Count > 0)
        {
            for (int i = 0; i < mPropItemList.Count; i++)
            {
                if (mPropItemList[i].enumid == index)
                {
                    PropItemTable info = new PropItemTable();
                    info = mPropItemList[i];
                    return info;
                }
            }
        }
        else
        {
            OnLoadPropIconTable();
            for (int i = 0; i < mPropItemList.Count; i++)
            {
                if (mPropItemList[i].enumid == index)
                {
                    return mPropItemList[i];
                }
            }
        }

        return null;
    }
    public static PropItemTable OnGetPropItemTableById(int itemid)
    {
        if (mPropItemList.Count > 0)
        {
            for (int i = 0; i < mPropItemList.Count; i++)
            {
                if (mPropItemList[i].Id == itemid)
                {
                    PropItemTable info = new PropItemTable();
                    info = mPropItemList[i];
                    return info;
                }
            }
        }
        else
        {
            OnLoadPropIconTable();
            for (int i = 0; i < mPropItemList.Count; i++)
            {
                if (mPropItemList[i].Id == itemid)
                {
                    return mPropItemList[i];
                }
            }
        }

        return null;
    }
    public static PropItemTable OnGetPropItemTableById(PropItemEnum index)
    {
        if (mPropItemList.Count > 0)
        {
            for (int i = 0; i < mPropItemList.Count; i++)
            {
                if (mPropItemList[i].enumid == index)
                {
                    return mPropItemList[i];
                }
            }
        }
        else
        {
            OnLoadPropIconTable();
            for (int i = 0; i < mPropItemList.Count; i++)
            {
                if (mPropItemList[i].enumid == index)
                {
                    PropItemTable info = new PropItemTable();
                    info = mPropItemList[i];
                    return info;
                }
            }
        }

        return null;
    }

    #endregion


    #region 设置当前关卡等级

    public static int OnGetCurTypeLevel(bool set = false)
    {
        int CurLevel = 1;
        string curnameType1 = "CUR_TYPE_LEVEL_1_1";
        string curnameType2 = "CUR_TYPE_LEVEL_2_1";
        if (set)
        {
            PlayerPrefs.SetInt(GameHelp.GameType == 1?curnameType1:curnameType2,GameHelp.GameLevel);
        }
        else
        {
            if (GameHelp.GameType == 1)
            {
                if (PlayerPrefs.HasKey(curnameType1))
                {
                    CurLevel = PlayerPrefs.GetInt(curnameType1);
                }
            }
            else
            {
                if (PlayerPrefs.HasKey(curnameType2))
                {
                    CurLevel = PlayerPrefs.GetInt(curnameType2);
                }
            }
        }

        return CurLevel;
    }

    #endregion
    
    //跟新用户金币
    public static void OnUpdatePlayerGold(int add)
    {
        mPlayerGold += add;
        OnGetLocalGold(mPlayerGold);
        EventCenter.BroadCast(EventUtils.EventType.EVNET_UPDATE_PLAYERINFO);
    }
    
    //跟新用户体力
    public static void OnUpdatePlayerPower(int add)
    {
        if (add <= -1 && mPlayerPower == mPlayerPowerMax)
        {
            OnSetDepotPowerTime();
        }
        
        mPlayerPower += add;
        OnGetLocalPower(mPlayerPower);
        EventCenter.BroadCast(EventUtils.EventType.EVNET_UPDATE_PLAYERINFO);
    }
}
